//
//  EventAddMemberController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/26/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "EventAddMemberController.h"

@interface EventAddMemberController ()
@property(nonatomic,retain) NSString *key ;
#define allSpaceCheck( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]

@end

@implementation EventAddMemberController
@synthesize topNavi,noteFace,noteContact,noteRecent,addEventTable,listData,searchBar,createContact,setKeyList,sourceArray,recent,contace,facebook,indexPress,key, flagSendPage, listNew, listRecent;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(IBAction)goto:(id)sender
{
    EventDetailController *page =[[[EventDetailController alloc]init]autorelease];
[self presentModalViewController:page animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageDataArray =[[NSMutableArray alloc]init];
    
    [StyleControl background:self.view];
    setKeyList =[[NSMutableDictionary alloc]init];
    
    if(self.tempArray.count)
    {
    setKeyList=self.tempArray;
    }
    listData =[[NSMutableArray alloc]init];
    sourceArray=[[NSMutableArray alloc]init];
    
    User *user = [User instance];
    listRecent = [[NSMutableArray alloc] init];
    listRecent = [user getRecentUser:user._id];
    NSLog(@"get recent in load %d", [listRecent count]);
    [self createNaviStyle];
    [self createSearch];
    [self createTopNavi];
    


}

-(void)popUpClick:(UITapGestureRecognizer  *)gesture
{    
}


-(IBAction)eventList:(id)sender
{
    EventListController *detail = [[EventListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}

-(IBAction)saveMember:(id)sender
{
    [listData removeAllObjects];
    NSArray *allKeys =[setKeyList allKeys];
    
    ParticipateClass *unit;
    for(int i = 0 ; i< setKeyList.count; i++)
    {
        unit = [setKeyList objectForKey:[allKeys objectAtIndex:i]];
        if(unit.active)
        {
            [listData addObject:unit];
        }
    }
    if (flagSendPage) {
        EventEditMemberController *detail = [[EventEditMemberController alloc] init];
        detail.listNew=[[NSMutableArray alloc]init];
        if([listNew count]){
            for(int i=0;i<[listNew count];i++){
                [listData addObject:[listNew objectAtIndex:i]];
            }
        }
        
        detail.listNew=listData;
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }else{
        EventAddPreviewController *detail = [[EventAddPreviewController alloc] init];
        detail.listData=[[NSMutableArray alloc]init];
        detail.tempArray=[[NSMutableDictionary  alloc]init];
        detail.listData=listData;
        detail.tempArray=setKeyList;
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }
    
    
}
-(void)createNaviStyle
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *right =[UIButton buttonWithType:UIButtonTypeCustom];
      [right setTitle:@"Gereed" forState:(UIControlStateNormal)];
    
    [left addTarget:self action:@selector(eventList:) forControlEvents:UIControlEventTouchUpInside];
    [right addTarget:self action:@selector(saveMember:) forControlEvents:UIControlEventTouchUpInside];
    
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 37, 35);
    right.frame = CGRectMake(250, 8, 60, 35);
    [right setTitle:@"Gereed" forState:UIControlStateNormal];
    [StyleControl  naviStyle:self.view andTitle:@"Nodig uit" andColor:0 andleftButton:left andRightButton:right borderLeft:1 borderRight:1];
}
-(void)createListDataAddress
{
    [self addressAuthentication];
}
-(void)addressAuthentication
{
    NSString *const kDenied = @"Access to address book is denied";
    NSString *const kRestricted = @"Access to address book is restricted";
    
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook;
    switch (ABAddressBookGetAuthorizationStatus())
    { case kABAuthorizationStatusAuthorized:
        
    {
        addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        
         [self readFromAddressBook:addressBook];

        if (addressBook != NULL)
        {
            CFRelease(addressBook);
        }
        break;
    }
        case kABAuthorizationStatusDenied:
        { [self displayMessage:kDenied];
            break;
        }
        case kABAuthorizationStatusNotDetermined:
        {
            addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRequestAccessWithCompletion
            (addressBook, ^(bool granted, CFErrorRef error)
            {
                if (granted)
                {
                    NSLog(@"Access was granted");
                } else {
                    NSLog(@"Access was not granted");
                }
                if (addressBook != NULL){
                    CFRelease(addressBook); }
            });
            break; }
        case kABAuthorizationStatusRestricted:{ [self displayMessage:kRestricted]; break;
        } }
}
- (void) displayMessage:(NSString *)paramMessage
{
    
}

-(void)readFromAddressBook:(ABAddressBookRef)paramAddressBook
{    
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(paramAddressBook);
    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
                                                               CFArrayGetCount(people),
                                                               people);
    
    CFArraySortValues(peopleMutable,
                      CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                      (CFComparatorFunction) ABPersonComparePeopleByName,
                      kABPersonSortByFirstName);
    
    // or to sort by the address book's choosen sorting technique
    //
    // CFArraySortValues(peopleMutable,
    //                   CFRangeMake(0, CFArrayGetCount(peopleMutable)),
    //                   (CFComparatorFunction) ABPersonComparePeopleByName,
    //                   (void*) ABPersonGetSortOrdering());
    
    CFRelease(people);
    
    // If you don't want to have to go through this ABRecordCopyValue logic
    // in the rest of your app, rather than iterating through doing NSLog,
    // build a new array as you iterate through the records.
    
    for (CFIndex i = 0; i < CFArrayGetCount(peopleMutable); i++)
    {
        ABRecordRef record = CFArrayGetValueAtIndex(peopleMutable, i);
        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonFirstNameProperty));
        NSString *lastName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonLastNameProperty));
        NSLog(@"person = %@, %@", lastName, firstName);
    }
    
    CFRelease(peopleMutable);
    
    //////
    
    [listData removeAllObjects];
    [sourceArray removeAllObjects];
    NSArray *allPeople = ( NSArray *)
    ABAddressBookCopyArrayOfAllPeople(paramAddressBook);
    NSUInteger peopleCounter = 0;
    for (peopleCounter = 0;peopleCounter < [allPeople count]; peopleCounter++)
    {
        ParticipateClass *unit=[[ParticipateClass alloc]init];
        ABRecordRef thisPerson = ( ABRecordRef) [allPeople objectAtIndex:peopleCounter];
        NSString *firstName = ( NSString *) ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty);
       // NSString *lastName = ( NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);
        NSString *email = ( NSString *) ABRecordCopyValue(thisPerson, kABPersonEmailProperty);
        NSString *eMailDetail= (NSString*)ABMultiValueCopyValueAtIndex (email,0);

        unit.name=firstName;//[firstName stringByAppendingString:[NSString stringWithFormat:@" %@",lastName]];
        unit.firstMail =eMailDetail;
        unit.image = @"contact";
        unit.idString = @"";
        if(unit.firstMail)
        {
            NSData  *imgData = (NSData *)ABPersonCopyImageDataWithFormat(thisPerson,kABPersonImageFormatThumbnail);
            UIImage  *image = [UIImage imageWithData:imgData];
            
            if(image)
            {
                [self.imageDataArray addObject:imgData];
            }
            else
            {
                [self.imageDataArray addObject:@"load Image default"];
            }
            
            [listData addObject:unit];
            [sourceArray addObject:unit];
        }

    }
}

-(void)createListDataFaceBook
{
    [listData removeAllObjects];
    [sourceArray removeAllObjects];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *friends= [defaults objectForKey:@"friendList"];
    
    for (int i = 0 ; i < friends.count; i++)
    {
        ParticipateClass *unit =[[ParticipateClass alloc]init];

        NSDictionary *temp= [friends objectAtIndex:i];
        unit.idString=[temp objectForKey:@"id"];
        unit.name=[temp objectForKey:@"name"];
        unit.image=@"";
        [listData addObject:unit];
        [sourceArray addObject:unit];
    }
    
    self.friendCounterSearch = sourceArray.count;
}

-(void)createListDataRecent
{
    //NSLog(@"count item %d", [listRecent count]);
    [listData removeAllObjects];
    [sourceArray removeAllObjects];
    /*for(int i = 0 ; i<5 ; i++)
    {
        ParticipateClass *unit=[[ParticipateClass alloc]init];
        unit.name =[NSString stringWithFormat:@"recent %d",i];
        [sourceArray addObject:unit];
        [listData addObject:unit];
    }*/
    for(int i=0;i<[listRecent count];i++)
    {
        [listData addObject:[listRecent objectAtIndex:i]];
    }
    //listData = listRecent;
    //sourceArray = listData;
    
}

-(void)createKeyList
{
    for(int i = 0 ; i<listData.count; i++)
    {
        [setKeyList setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:i]];
    }
    
}
-(void)createSearch
{
    searchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(5, 50, 310, 44)];
    [[[searchBar subviews] objectAtIndex:0] setAlpha:0.0];
    searchBar.barStyle=UIBarStyleBlackOpaque;
    for (UIView *searchBarSubview in [searchBar subviews]) {
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
            @try {
                
                [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
            }
            @catch (NSException * e) {
            }
        }
    }
    searchBar.delegate=self;
    searchBar.showsCancelButton=YES;
    
    UIButton *cancelButton = nil;
    for(UIView *subView in searchBar.subviews){
        if([subView isKindOfClass:UIButton.class]){
            cancelButton = (UIButton*)subView;
            [cancelButton setTitle:@"Done" forState:UIControlStateNormal];
            [cancelButton   addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    [self.view addSubview:searchBar];
}

-(void)cancelPress
{
    [searchBar resignFirstResponder];

    
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];

    return YES;
}// return NO to not resign first responder

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];

}
-(void)getDataCount:(NSInteger)index andText:(NSString *)searchText
{
    
    [listData removeAllObjects];
    if([allSpaceCheck(searchText) length]!=0)
    {
        searchText = [searchText lowercaseString];
        ParticipateClass *unit;
        [listData removeAllObjects];
        for(int i= 0 ; i<sourceArray.count  ; i++)
        {
            unit    =[sourceArray objectAtIndex:i];
            unit.name =[unit.name lowercaseString];
            unit.firstMail=[unit.firstMail lowercaseString];
            NSRange mName;
            NSRange mMail;
            mName = [unit.name rangeOfString: searchText];
            mMail =[unit.firstMail rangeOfString: searchText];
            
            if((mName.location <unit.name.length) || (mMail.location <unit.firstMail.length))
            {
                searchText = allSpaceCheck(searchText);
                
                [listData addObject:[sourceArray objectAtIndex:i]];
            }
         
        }
        if(index==0)
        {
            
            [recent setTitle:[NSString stringWithFormat:@"Recent(%d)",listData.count] forState:UIControlStateNormal];
            [recent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        else if(index==1)
        {
            
            [contace setTitle:[NSString stringWithFormat:@"Contacten(%d)",listData.count] forState:UIControlStateNormal];
            [contace setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        }
        else if(index == 2)
        {
            
            [facebook setTitle:[NSString stringWithFormat:@"Facebook(%d)",listData.count] forState:UIControlStateNormal];
            [facebook setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        }

    
   
    }

    else
        {
            if(index==0)
            {
                
                [recent setTitle:@"Recent" forState:UIControlStateNormal];
                [self createListDataRecent];
            }
            else if(index==1)
            {
                
                [contace setTitle:@"Contacten" forState:UIControlStateNormal];
    
                [self createListDataAddress];
            }
            else if(index == 2)
            {
                
                [facebook setTitle:@"Facebook" forState:UIControlStateNormal];
                [self createListDataFaceBook];
            }

        }
    [addEventTable reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    [self createListDataRecent];
    [self getDataCount:0 andText:searchText];
    
    [self createListDataAddress];
    [self getDataCount:1 andText:searchText];
    
    [self createListDataFaceBook];
    [self getDataCount:2 andText:searchText];
    
    
    switch (indexPress)
    {
        case 0:
            [self createListDataRecent];    [self getDataCount:0 andText:searchText];
; break;
        case 1:
            [self createListDataAddress];    [self getDataCount:1 andText:searchText];
 break;
        case 2:
            [self createListDataFaceBook];    [self getDataCount:2 andText:searchText];
 break;
        default:break;
    }
    
}


-(void)styleTop:(UIView *)view
{
    view.layer.borderColor=[UIColor grayColor].CGColor;
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius=10;
    
    view.layer.shadowColor = [[UIColor blackColor] CGColor];
    view.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    view.layer.shadowOpacity = 1.0f;
    
}
-(void  )createTopNavi
{
    
    UIView *buttonContent=[[UIView  alloc]initWithFrame:CGRectMake(10, 100, 300, 35)];
    [self.view addSubview:buttonContent  ];
    [self styleTop:buttonContent];
    [buttonContent release];
    
    
    recent =[UIButton buttonWithType:UIButtonTypeCustom];
    recent.frame=CGRectMake(10, 100, 102, 35);
    [recent setTitle:@"Recent" forState:UIControlStateNormal];
    [recent addTarget:self action:@selector(indexTopNavi:) forControlEvents:UIControlEventTouchUpInside];
    recent.tag=100;
    [self styleButton:recent];
    [self.view addSubview:recent];
    
    facebook =[UIButton buttonWithType:UIButtonTypeCustom];
    facebook.frame=CGRectMake(208, 100, 102, 35);
    [facebook setTitle:@"FaceBook" forState:UIControlStateNormal];
    [facebook addTarget:self action:@selector(indexTopNavi:) forControlEvents:UIControlEventTouchUpInside];
    facebook.tag=300;
    [self styleButton:facebook];
    [self.view addSubview:facebook];
    
    contace =[UIButton buttonWithType:UIButtonTypeCustom];
    contace.frame=CGRectMake(105, 100, 110, 35);
    [contace setTitle:@"Contacten" forState:UIControlStateNormal];
    [contace addTarget:self action:@selector(indexTopNavi:) forControlEvents:UIControlEventTouchUpInside];
    contace.tag=200;
    [self styleButton:contace];
    contace.layer.cornerRadius=0;
    [contace setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:contace];
    
   }

-(void)styleButton:(UIButton *)button
{
    [button setBackgroundColor:[UIColor whiteColor]];
    button.layer.borderColor = [UIColor colorWithRed:0.278 green:0.275 blue:0.275 alpha:1.0].CGColor;
    button.layer.borderWidth = 1.0f;
    button.layer.cornerRadius = 8;
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    button.titleLabel.textColor=[UIColor blackColor];
}

-(void)setActiveButton:(NSInteger)index
{
    for(int i = 1 ;i<4;i++)
    {
        UIButton *button =(UIButton *)[self.view viewWithTag:i*100];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        if(index==i*100)
        {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            [button setBackgroundColor:[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1]];

        }
    }
        
}
-(IBAction)indexTopNavi:(id)sender
{
    NSInteger indexNavi =[(UIButton *)sender tag]/100-1;
    indexPress = indexNavi;
    [self setActiveButton:[(UIButton *)sender tag]];
    
    NSLog(@"index press : %d",indexNavi);

    
    if(indexNavi== 0)
    {
        [noteFace setHidden:YES];
        [noteContact setHidden:YES];
        [self createTable:0];
        
    }
    else if(indexNavi==1)
    {
        // Contact
        
        if(createContact)
        {
            [noteFace setHidden:YES];
            [noteFace setHidden:YES];
        }
        else
        {
            
            [noteFace setHidden:YES];
            [noteRecent setHidden:YES];
            [self createTable:1];
        }
    }
    else
    {
        [noteContact setHidden:YES];
        [noteRecent setHidden:YES];
        [self createTable:2];
    }
    
    [self searchBar:searchBar textDidChange:searchBar.text];

    
}

-(void)styleNote:(UIView *)view
{
    view.layer.borderColor=[UIColor grayColor].CGColor;
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius=10;
}

-(void)createTable:(NSInteger) indexData
{
    NSLog(@"create table");
    [addEventTable removeFromSuperview];
    addEventTable =[[UITableView alloc]initWithFrame:CGRectMake(10, 150, 300, 300)];
    addEventTable.delegate= self;
    addEventTable.dataSource=self;
    [self styleNote:self.addEventTable];
    [self.view addSubview:self.addEventTable];
    
    if(!indexData)
    {
        [self createListDataRecent ];
    }
    else if(indexData==1)
    {
        [self createListDataAddress];
    }
    else
    {
        [self createListDataFaceBook];
        
        
        AppDelegate *appdelegate =[[UIApplication sharedApplication]delegate];
        
        
        if(appdelegate.isFace==1)
        {
        
            
            [appdelegate.imageUrlArray removeAllObjects];
            for(int i = 0 ; i< listData.count; i++)
            {
                NSString *idString  =[[NSString alloc]init];
                idString =[[listData objectAtIndex:i] idString];
                [appdelegate.imageUrlArray addObject:idString];
                
            }
            
            if(appdelegate.threadStart==0)
            {
                appdelegate.threadStart =1;
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                [NSThread detachNewThreadSelector:@selector(threadStart) toTarget:self withObject:nil];
                [pool release];
            }
        }
    }
}


- (void)threadStart
{
 
    [NSThread sleepForTimeInterval:1];
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [NSThread detachNewThreadSelector:@selector(getImageData) toTarget:self withObject:nil];
    [pool release];

}
- (void)getImageData
{
    AppDelegate *appdelegate=[[UIApplication sharedApplication]delegate];
    for(int i = 0; i< appdelegate.imageUrlArray.count; i++)
    {
        if(appdelegate.isFace==1)
        {
        
        NSString  *idString =[appdelegate.imageUrlArray objectAtIndex:i];
        NSString *urlString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture",idString];
        
        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlString]];
            
       FasterImage *unit =[[FasterImage alloc]init];
           unit.idFasterImage=idString;
           unit.imageData =imageData;
       
      [appdelegate.imageDataArray addObject:unit];
            }
        else
        {
            [appdelegate.imageDataArray removeAllObjects];
            [appdelegate.imageUrlArray removeAllObjects];
            break ;
        }

    }
    
       

    appdelegate.threadStart=0;
    return;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return listData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EventAddMemberCell *cell = [[[EventAddMemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.addMemberButton.tag = indexPath.row;
    [cell.addMemberButton addTarget:self action:@selector(pressCheck:) forControlEvents:UIControlEventTouchUpInside];
    [cell.addMemberButton setBackgroundImage:[UIImage imageNamed:@"friendInvite"] forState:UIControlStateNormal];
    

    if(indexPress==0)
    {
        key =[@"Recent" stringByAppendingString:[NSString stringWithFormat:@"%d",indexPath.row]];
        
    }
    else if(indexPress==1)
    {
        key =[@"Address" stringByAppendingString:[NSString stringWithFormat:@"%d",indexPath.row]];
    }
    else
    {
        key =[@"Facebook" stringByAppendingString:[NSString stringWithFormat:@"%d",indexPath.row]];
    }

    if([[setKeyList objectForKey:key] active])
    [cell.addMemberButton setBackgroundImage:[UIImage imageNamed:@"friendAccept"] forState:UIControlStateNormal];

    [StyleControl setBorderStyle:cell.imageProfile];

    ParticipateClass *unitClass =[[ParticipateClass alloc]init];

    unitClass =[listData objectAtIndex:indexPath.row];
    
    
    /*
    [Base64 initialize];
    
    NSData* data = [Base64 decode:unitClass.image ];
      */
    
    

    AppDelegate *appdelegate =[[UIApplication sharedApplication]delegate];
   if(indexPath.row<  appdelegate.imageDataArray.count && indexPress ==2)
    {

       

        if(searchBar.text.length ==0)
        {
            FasterImage *imageUnit =[appdelegate.imageDataArray objectAtIndex:indexPath.row];

            cell.imageProfile.image= [UIImage imageWithData:(NSData*)imageUnit.imageData];
        }
        else
        {
          

            for( int i = 0 ; i< appdelegate.imageDataArray.count ; i++)
            {
                FasterImage *imageUnit =[appdelegate.imageDataArray objectAtIndex:i];
                
                if( [imageUnit.idFasterImage isEqualToString:unitClass.idString])
                    cell.imageProfile.image= [UIImage imageWithData:(NSData*)imageUnit.imageData];

            }
        }
 
    }
    
    
    else if(indexPath.row <self.imageDataArray.count && indexPress==1)
    {
        if([[self.imageDataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
        {
       
        }
        else 
        {
              cell.imageProfile.image= [UIImage imageWithData:(NSData*)[self.imageDataArray   objectAtIndex:indexPath.row]];
        }
    }
    else if(indexPress==0)
    {
        cell.imageProfile.image= [Helper getImageMember:unitClass];
    }
   
    cell.name.text=unitClass.name;
    cell.firstMail.text=unitClass.firstMail;
    return cell;
    
    
}

-(void) pressCheck  :(id) sender
{
    UIButton *button= (UIButton *)sender;
    
    if(indexPress==0)
    {
        key =[@"Recent" stringByAppendingString:[NSString stringWithFormat:@"%d",button.tag]];
        
    }
    else if(indexPress==1)
    {
        key =[@"Address" stringByAppendingString:[NSString stringWithFormat:@"%d",button.tag]];
    }
    else
    {
        key =[@"Facebook" stringByAppendingString:[NSString stringWithFormat:@"%d",button.tag]];
    }
    
    ParticipateClass *unit =[[ParticipateClass alloc]init];
    unit   = [listData objectAtIndex:button.tag];
    
    if([[setKeyList objectForKey:key]active])
    {
        
        [setKeyList removeObjectForKey:key];
        unit.active=0;
        [setKeyList setObject:unit forKey:key];
        [button setBackgroundImage:[UIImage imageNamed:@"friendInvite"] forState:UIControlStateNormal];
        
    }
    else
    {
        [setKeyList removeObjectForKey:key];
        unit.active=1;
        [setKeyList setObject:unit forKey:key];
        [button setBackgroundImage:[UIImage imageNamed:@"friendAccept"] forState:UIControlStateNormal];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [super dealloc];
    [addEventTable release];
    [topNavi release];
    [noteContact release];
}
- (void)viewDidUnload {
    [super viewDidUnload];
}
@end
